/**
 * 发表评价
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/28
 */
var self = id = uid = title =  '';

var index = 1;

var files = [];  

document.addEventListener('plusready' , ready ,  false);

function ready(){
	self = plus.webview.currentWebview();
	uid = self.uid;
	id = self.aid; 
	title = self.title;
	nickname = self.nickname;
	businessid = self.businessid;
	//星级的选择
	$('.xingxing span').on('click' , function(){
		$(this).nextAll().addClass("color-c");
		$(this).removeClass("color-c").prevAll().removeClass("color-c");
	})
	
	//上传图片
 	$('.upload').on('click' , function(){
 		plus.nativeUI.confirm('需要打开相册吗?' , function(e){
 			if(e.index == 0){
		 		plus.nativeUI.showWaiting('图片处理中...');
		 		plus.gallery.pick(function (e){
					for(var i in e.files){ 
						if(i < 3){
							if($('img').size() < 3){
								appendPic(e.files[i] , i); 
							}else{
								toast('最多只能上传3张哦');
							}
						}else{
							toast('最多只能上传3张哦');
						}
					} 
				}, function(e){},{multiple:true});
 			}
 		} , '第三方咨询' , ['确定' , '取消']);
 	})
 	 
 	
 	//提交
 	$('.add_evaluate').on('click' , function(){
 		var level = 5 - parseInt($('span').find('.color-c').length);
 		
 		var body = $('.body').val();
 		if(!body.trim()){
 			toast('内容不能为空');return;
 		}
 		var pic = ''
 		
 		$('.pic img').each(function(index){
 			
 			pic += pic == 0 ? $(this).attr('data-src') + '|,|' : '|-|' + $(this).attr('data-src');
 			
 		})
 		
		plus.nativeUI.showWaiting('发表中...');
 		
	 	$.ajax({  
	 		
			type:"post", 
			
			url:apiRoot + '/Home/Message/addEvaluate/',
			
			data:{
				level : level,
				uid : uid , 
				tid : 36,
				isshow : 1,
				title :  nickname +'在' + new Date().toLocaleString() + '评价了<<' + title +'>>该商家',
				pic : pic ,
				user:nickname,
				business_id : businessid,
				order_id : id,
				body : body
			},
			
			dataType : 'json',
			
			success : function(data){ 
				
				plus.nativeUI.closeWaiting();  
				
				if(data >= 1){
					
					toast('评价成功,积分将发放到您的账户里');
					
					self.close(); 
					
				}else if(data == -1){
					
					toast('评价失败，您已评价过该订单');
					
				}
				
			},
			error : function(e){
				console.log(JSON.stringify(e))
				toast('连接超时,请检查网络');
				plus.nativeUI.closeWaiting();
			}
			
		});
 		
 		
 	})
 	
}

// 添加照片
function appendPic(p , i) {  
	
	var newPath = p.replace(/\./g , new Date().getTime()+'.');
	
	plus.zip.compressImage({
		
			src:p,
			
			dst:newPath,
			
			quality:20
			
		},
		
		function() {
			
			files.push({name:"uploadkey"+index,path:newPath});
			
			index++;
			
			upload(i);
			
		},function(error) {
			
			toast('压缩图片失败');
			
	});
}

function upload(i){
	
	if(files.length <= 0){  
		
		toast("请上传头像！");
		
		return;
		
	}
	
	var server = apiRoot+'/Home/Member/uploadPic/'; 
	
	var task = plus.uploader.createUpload(server,
		
		{method:"POST"},
		
		function(t,status){ //上传完成
			
			if(status == 200){
				
				toast('处理完成');
				
				$('.pic').prepend('<div class="image-item"><div class="image-close">X</div><img data-src ="'+t.responseText+'"  src="'+getImgUrl(t.responseText)+'"></div>');
				
 				$('.image-close').on('click' , function(){ $(this).parent().remove(); })
 				
 				if($('img').size() <= 3){
 					
 					plus.nativeUI.closeWaiting();
 				}
				
			}else{
				
				plus.nativeUI.alert("上传失败："+status);
				
			}
		} 
	);
	
	for(var i = 0;i < files.length ; i++){
		
		var f = files[i];
		
		task.addFile(f.path,{key:f.name});
		
	} 
	
	task.start();
}


/**
 * 获取积分
 * @param {Object} uid
 * @param {Object} type
 * @param {Object} field
 */
function integral(uid , type , field) {  
	
	$.ajax({  
	 		
		type:"post", 
		
		url:apiRoot + '/Home/Member/integral/',
		 
		data:{
			uid : uid , 
			type : type,
			field : field
		},
		
		dataType : 'json',
		
		success : function(data){ 
			plus.nativeUI.closeWaiting();  
			if(data >= 1){
				toast('积分已发放到您的账户里');
			}
		},
		error : function(e){
			console.log(JSON.stringify(e))
			toast('连接超时,请检查网络');
			plus.nativeUI.closeWaiting();
		}
		
	});
 		
}